﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ScrumTracker.Models;

namespace ScrumTracker.Helpers
{
    public class SessionHelper
    {
        public static List<Product> Products
        {
            get
            {
                using (var context = new ScrumTrackerEntities())
                {
                    context.Products.MergeOption = System.Data.Objects.MergeOption.OverwriteChanges;
                    var products = context.Products.Include("Sprints").Where(p => (p.IsDeleted ?? false).Equals(false)).Select(p => p).ToList();
                    return products;
                }
            }
        }

        public static User CurrentUser
        {
            get
            {
                if (HttpContext.Current.Session["user"] == null)
                {
                    //var products = ScrumTrackerEntities.Instance.Products.Include("user").ToList();
                    //HttpContext.Current.Session["user"] = products;
                    //return products;
                    return new User() { UserId = 1 };
                }
                else
                {
                    //return HttpContext.Current.Session["user"] as List<Product>;
                    return new User() { UserId = 1 };
                }
            }
        }

        public static Product CurrentProduct
        {
            get
            {
                using (var context = new ScrumTrackerEntities())
                {
                    context.Products.MergeOption = System.Data.Objects.MergeOption.OverwriteChanges;
                    return context.Products.Include("Sprints").SingleOrDefault(p => p.ProductId == CurrentProductId);
                }
            }
        }

        public static int? CurrentProductId
        {
            get
            {
                return HttpContext.Current.Session["product_id"] as int?;
            }
            set
            {
                HttpContext.Current.Session["product_id"] = value;
            }
        }
    }
}